﻿@using Microsoft.JSInterop
@using Covenant.Models.Covenant
@using Covenant.Components.Shared
@inject IJSRuntime IJSRuntime

<CovenantTable TItem="CapturedHashCredential" Items="HashCredentials.ToList()" SearchRow="IsRowDisplayed"
               IsSearchable="IsSearchable" IsSortable="IsSortable" IsPaginated="IsPaginated" PageLength="PageLength" class="table table-hover">
    <TableHeader>
        <SortableTableHeader TItem="CapturedHashCredential" TItem2="string" GetSortableProperty="(credential) => credential.Domain">
            <HeaderContent>Domain</HeaderContent>
        </SortableTableHeader>
        <SortableTableHeader TItem="CapturedHashCredential" TItem2="string" GetSortableProperty="(credential) => credential.Username">
            <HeaderContent>Username</HeaderContent>
        </SortableTableHeader>
        <SortableTableHeader TItem="CapturedHashCredential" TItem2="HashType" GetSortableProperty="(credential) => credential.HashCredentialType">
            <HeaderContent>HashCredentialType</HeaderContent>
        </SortableTableHeader>
        <SortableTableHeader TItem="CapturedHashCredential" TItem2="string" GetSortableProperty="(credential) => credential.Hash">
            <HeaderContent>Hash</HeaderContent>
        </SortableTableHeader>
    </TableHeader>
    <TableRow Context="credential">
        <tr>
            <td>@credential.Domain</td>
            <td>@credential.Username</td>
            <td>@credential.HashCredentialType</td>
            <td>@credential.Hash</td>
        </tr>
    </TableRow>
</CovenantTable>

@code {
    [Parameter]
    public IEnumerable<CapturedHashCredential> HashCredentials { get; set; }

    [Parameter]
    public bool IsSearchable { get; set; } = false;

    [Parameter]
    public bool IsSortable { get; set; } = true;

    [Parameter]
    public bool IsPaginated { get; set; } = true;

    [Parameter]
    public int PageLength { get; set; } = 10;

    private bool IsRowDisplayed(CapturedHashCredential credential, string SearchTerm)
    {
        return SearchTerm == string.Empty ||
            credential.Domain.Contains(SearchTerm, StringComparison.CurrentCultureIgnoreCase) ||
            credential.Username.Contains(SearchTerm, StringComparison.CurrentCultureIgnoreCase) ||
            credential.HashCredentialType.ToString().Contains(SearchTerm, StringComparison.CurrentCultureIgnoreCase) ||
            credential.Hash.Contains(SearchTerm, StringComparison.CurrentCultureIgnoreCase);
    }
}